package com.example.noobpackage.ui.selfpackage

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.navigation.fragment.FragmentNavigator
import androidx.navigation.fragment.FragmentNavigatorExtras
import androidx.recyclerview.widget.RecyclerView
import com.example.noobpackage.R
import com.example.noobpackage.logic.model.ExpressData
import com.example.noobpackage.tool.ExpressIconUtil
import com.example.noobpackage.tool.ItemTouchHelperAdapter
import com.example.noobpackage.ui.MainViewModel

class SelfPackageAdapter :
    RecyclerView.Adapter<SelfPackageAdapter.ViewHolder>(), ItemTouchHelperAdapter {
    lateinit var myItemClickListener: (Int, FragmentNavigator.Extras) -> Unit
    lateinit var onItemDismissCallback: (Int) -> Unit
    private var packagesList: List<ExpressData> = listOf()

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        // 补全之后快递图片
        val image: ImageView = view.findViewById(R.id.typeImage)
        val takingCode: TextView = view.findViewById(R.id.takingCode)
        val takingPlace: TextView = view.findViewById(R.id.takingPlace)
        val item: View = view.findViewById(R.id.my_package_item_card)
        val packageState: TextView = view.findViewById(R.id.package_status)
    }

    fun setExpressData(newData: List<ExpressData>) {
        packagesList = newData
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.self_package_item_card, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val packageData = packagesList[position]
        if (packageData.isPublished) {
            holder.packageState.visibility = View.VISIBLE
            if (packageData.isAccepted) holder.packageState.text = "已接单"
        } else {
            holder.packageState.visibility = View.INVISIBLE
        }
        // 设置快递图标
        holder.image.setImageResource(ExpressIconUtil.getIconResource(packagesList[position].expressType))

        holder.takingCode.text = packageData.expressTakingNumber
        holder.takingPlace.text = packageData.expressLocation
        // 配置不同的transitionName对应列表项
        holder.takingCode.transitionName = "takingCode$position"
        holder.takingPlace.transitionName = "takingPlace$position"
        holder.image.transitionName = "typeImage$position"

        // 根据快递类型实现不同的跳转
        holder.item.setOnClickListener {
            // 生成跳转的extra，让外部接口进行跳转
            val extras = FragmentNavigatorExtras(
                holder.takingCode to "takingCode$position",
                holder.takingPlace to "takingPlace$position",
                holder.image to "typeImage$position"
            )
            myItemClickListener(position, extras)
        }

    }

    override fun getItemCount(): Int {
        return packagesList.size
    }

    override fun onItemDismiss(position: Int) {
        // 判断不同类型实现不同的删除逻辑
        if (!packagesList[position].isAccepted && !packagesList[position].isPublished) {
            onItemDismissCallback(position)
        }
    }

    override fun onItemMove(fromPosition: Int, toPosition: Int) {}

    fun setItemClick(itemClickListener: (Int, FragmentNavigator.Extras) -> Unit) {
        myItemClickListener = itemClickListener
    }

    override fun getIsPublished(position: Int): Boolean {
        return packagesList[position].isPublished
    }
}